package com.fita.wechat.service.impl;

import com.fita.entity.agent.AgentApply;
import com.fita.entity.agent.AgentCompany;
import com.fita.wechat.dao.AgentApplyDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fita.entity.agent.Agent;
import com.fita.wechat.dao.AgentDao;
import com.fita.wechat.service.AgentService;

import java.util.List;

/**
 * 
 * @author dan
 *
 */
@Service("agentService")
public class AgentServiceImpl implements AgentService{
    
    @Dao
    private AgentDao agentDao;

    @Dao
    private AgentApplyDao agentApplyDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.AgentService#load(java.lang.Long)
     */
    public Agent load(Long id) {
        return genericDaoSupport.load(Agent.class, id);
    }

    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.AgentService#createOrLoadByMobile(java.lang.String)
     */
    public Agent loadByMobile(String mobile) {
        return agentDao.load(mobile);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.AgentService#loadByOpenid(java.lang.String)
     */
    public Agent loadByOpenid(String openid) {
        return agentDao.loadByOpenid(openid);
    }

    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.AgentService#login(java.lang.String, java.lang.String)
     */
    public void login(String mobile, String openid) {
        Agent agent = agentDao.load(mobile);
        if (agent == null) {
            throw new BusinessException("尚未创建代理账号，请点击右下方注册代理");
        }
        
        // update agent's openid
        if(StringUtils.isNotBlank(openid)) {
            genericDaoSupport.update(agent.bindOpenid(openid));
        }
    }
    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.AgentService#update(com.fita.entity.agent.Agent)
     */
    public void update(Agent agent) {
        genericDaoSupport.update(agent);
    }

    /**
     * 列出二级代理
     *
     * @param primaryAgentId
     * @return
     */
    public List<Agent> listSecondaryAgent(Long primaryAgentId) {

        Agent primaryAgent = genericDaoSupport.load(Agent.class, primaryAgentId);

        List<Agent> secondaryAgents = agentDao.listSecondaryAgent(primaryAgent);

        return secondaryAgents;
    }

    /**
     * 列出二级代理申请
     *
     * @param primaryAgentId
     * @return
     */
    public List<AgentApply> listSecondaryAgentApply(Long primaryAgentId) {

        Agent primaryAgent = genericDaoSupport.load(Agent.class, primaryAgentId);

        List<AgentApply> secondaryAgentApplys = agentApplyDao.listSecondaryAgentApply(primaryAgent);

        return secondaryAgentApplys;
    }

    /**
     * 修改提成率
     *
     * @param primaryAgentId
     * @param secondaryAgentId
     * @param rate
     */
    public void changeSecondaryAgentCommissionRate(Long primaryAgentId, Long secondaryAgentId, Integer rate) {

        Agent secondaryAgent = genericDaoSupport.load(Agent.class, secondaryAgentId);
        AgentCompany secondaryAgentCompany = secondaryAgent.getCompany();
        if (secondaryAgentCompany.getPrimaryAgent().getId().equals(primaryAgentId)){
            genericDaoSupport.update(secondaryAgentCompany.changeCommissionRate(rate));
        }else{
            throw new BusinessException("500", "操作失败");
        }
    }

}
